# @Version : 1.0
# @Author  : wxz
# @File    : user_chat_dao.py
# @Time    : 2025/6/20 20:47
# @Describe: 描述...
from typing import Any, Type

from fastapi import Depends
from sqlalchemy.exc import NoResultFound, MultipleResultsFound
from sqlmodel import select, Session

from apps.chat.data_model.user_chat_model import UserChatModel
from config.app_logger import logger
from database.mysql.mysql_client import MySQLClient


class UserChatDao:

    def __init__(self, db_session: Session = Depends(MySQLClient.get_db_session) ):
        self.db_session = db_session

    def find_user_chat(self, user_id, conversation_id)-> UserChatModel | None:

        """
        根据 conversation_id 会话ID 查询用户的会话
        @param user_id: 用户ID
        @param conversation_id: 会话ID
        @return: UserChatModel | None
        """

        try:
            stmt = (select(UserChatModel)
                    .where(UserChatModel.user_id == user_id)
                    .where(UserChatModel.conversation_id == conversation_id)
                    .where(UserChatModel.is_delete == 0)
                    )
            user_chat_model = self.db_session.exec(stmt).one()
        except NoResultFound as e1:
            return None
        except MultipleResultsFound as e2:
            return None

        return user_chat_model


    def save_user_chat(self, user_chat_model: UserChatModel):
        self.db_session.add(user_chat_model)
        self.db_session.flush()










